package com.probox.common.entity.service.order.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.order.OrderInfo;
import com.probox.common.entity.service.order.*;
import com.probox.order.api.domain.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.order.OrderComplaintMapper;
import com.probox.common.entity.domain.order.OrderComplaint;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户投诉Service业务层处理
 *
 * @author hds
 * @date 2021-06-07
 */
@Service
@Transactional
public class OrderComplaintServiceImpl extends ServiceImpl<OrderComplaintMapper, OrderComplaint> implements IOrderComplaintService {
    @Autowired
    private OrderComplaintMapper orderComplaintMapper;

    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private IOrderInfoItemService itemService;

    @Autowired
    private IOrderInvoiceService invoiceService;

    @Autowired
    private IOrderShipDeliverService deliverService;

    /**
     * 查询客户投诉
     *
     * @param complaintId 客户投诉ID
     * @return 客户投诉
     */
    @Override
    public OrderInfoVo selectOrderComplaintById(Long complaintId) {
        OrderInfoVo vo = null;
        OrderComplaint complaint = this.getById(complaintId);
        if(complaint!=null){
            Long orderUid = complaint.getOrderUid();
            OrderInfo orderInfo = orderInfoService.getById(orderUid);
            if(orderInfo!=null){
                vo = new OrderInfoVo();
                BeanUtils.copyProperties(orderInfo,vo);

                List<OrderItemVo> itemVoList = itemService.getByOrderUid(orderUid);
                if(itemVoList!=null && itemVoList.size()>0) {
                    vo.setItemList(itemVoList);
                }

                OrderInvoiceVo invoiceVo = invoiceService.getByOrderUid(orderUid);
                if(invoiceVo!=null) {
                    vo.setInvoice(invoiceVo);
                }

                List<OrderShipDeliverVo> deliverVoList = deliverService.getByOrderUid(orderUid);
                if(deliverVoList!=null && deliverVoList.size()>0) {
                    vo.setDeliverList(deliverVoList);
                }

                List<OrderComplaintVo> complaintVoList = new ArrayList<>();
                OrderComplaintVo complaintVo = new OrderComplaintVo();
                BeanUtils.copyProperties(complaint,complaintVo);
                complaintVoList.add(complaintVo);
                vo.setComplaintList(complaintVoList);

            }
        }
        return vo;
    }

    /**
     * 查询客户投诉列表
     *
     * @param orderComplaint 客户投诉
     * @return 客户投诉
     */
    @Override
    public List<OrderComplaint> queryList(OrderComplaint orderComplaint) {
        QueryWrapper<OrderComplaint> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getComplaintId())) && orderComplaint.getComplaintId() != null) {
            queryWrapper.eq("complaint_id", orderComplaint.getComplaintId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getOrderUid())) && orderComplaint.getOrderUid() != null) {
            queryWrapper.eq("order_uid", orderComplaint.getOrderUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getOrderNo())) && orderComplaint.getOrderNo() != null) {
            queryWrapper.like("order_no", orderComplaint.getOrderNo());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getComplaintType())) && orderComplaint.getComplaintType() != null) {
            queryWrapper.eq("complaint_type", orderComplaint.getComplaintType());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getComplaintTitle())) && orderComplaint.getComplaintTitle() != null) {
            queryWrapper.like("complaint_title", orderComplaint.getComplaintTitle());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getComplaintContent())) && orderComplaint.getComplaintContent() != null) {
            queryWrapper.eq("complaint_content", orderComplaint.getComplaintContent());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getComplaintImage())) && orderComplaint.getComplaintImage() != null) {
            queryWrapper.eq("complaint_image", orderComplaint.getComplaintImage());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getCustomerUid())) && orderComplaint.getCustomerUid() != null) {
            queryWrapper.eq("CUSTOMER_UID", orderComplaint.getCustomerUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getCustomerName())) && orderComplaint.getCustomerName() != null) {
            queryWrapper.like("CUSTOMER_NAME", orderComplaint.getCustomerName());
        }
        if(orderComplaint.getCreateDateArray()!=null && orderComplaint.getCreateDateArray().size()>0){
            queryWrapper.ge("create_date",orderComplaint.getCreateDateArray().get(0));
            queryWrapper.le("create_date",orderComplaint.getCreateDateArray().get(1)+" 23:59:59");
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getDataStatus())) && orderComplaint.getDataStatus() != null) {
            queryWrapper.eq("data_status", orderComplaint.getDataStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getProcessStatus())) && orderComplaint.getProcessStatus() != null) {
            queryWrapper.eq("process_status", orderComplaint.getProcessStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getProcessUser())) && orderComplaint.getProcessUser() != null) {
            queryWrapper.eq("process_user", orderComplaint.getProcessUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getProcessUserName())) && orderComplaint.getProcessUserName() != null) {
            queryWrapper.eq("process_user_name", orderComplaint.getProcessUserName());
        }
        if(orderComplaint.getProcessDateArray()!=null && orderComplaint.getProcessDateArray().size()>0){
            queryWrapper.ge("process_date",orderComplaint.getProcessDateArray().get(0));
            queryWrapper.le("process_date",orderComplaint.getProcessDateArray().get(1)+" 23:59:59");
        }
        if(orderComplaint.getFinishDateArray()!=null && orderComplaint.getFinishDateArray().size()>0){
            queryWrapper.ge("finish_date",orderComplaint.getFinishDateArray().get(0));
            queryWrapper.le("finish_date",orderComplaint.getFinishDateArray().get(1)+" 23:59:59");
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getProcessRemark())) && orderComplaint.getProcessRemark() != null) {
            queryWrapper.eq("process_remark", orderComplaint.getProcessRemark());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getUpdateUser())) && orderComplaint.getUpdateUser() != null) {
            queryWrapper.eq("update_user", orderComplaint.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getUpdateDate())) && orderComplaint.getUpdateDate() != null) {
            queryWrapper.eq("update_date", orderComplaint.getUpdateDate());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderComplaint.getUpdateName())) && orderComplaint.getUpdateName() != null) {
            queryWrapper.eq("update_name", orderComplaint.getUpdateName());
        }

        Long platFormUid = SecurityUtils.getPlatformId();
        if (platFormUid!=null && platFormUid>0)
            queryWrapper.eq("PLATFORM_UID",platFormUid);
        else
            throw new BaseException("账号异常");

        return orderComplaintMapper.selectList(queryWrapper);
    }

    /**
     * 新增客户投诉
     *
     * @param orderComplaint 客户投诉
     * @return 结果
     */
    @Override
    public boolean insertOrderComplaint(OrderComplaint orderComplaint) {

        return this.save(orderComplaint);

    }

    /**
     * 修改客户投诉
     *
     * @param orderComplaint 客户投诉
     * @return 结果
     */
    @Override
    public boolean updateOrderComplaint(OrderComplaint orderComplaint) {
        Date date = DateUtil.date();
        Long accountUid = SecurityUtils.getUserId();
        String accountName = SecurityUtils.getUsername();

        orderComplaint.setProcessUser(accountUid);
        orderComplaint.setProcessUserName(accountName);
        orderComplaint.setProcessDate(date);
        orderComplaint.setUpdateUser(accountUid);
        orderComplaint.setUpdateName(accountName);
        orderComplaint.setUpdateDate(date);
        return this.updateById(orderComplaint);
    }

    /**
     * 批量删除客户投诉
     *
     * @param complaintIds 需要删除的客户投诉ID
     * @return 结果
     */
    @Override
    public boolean deleteOrderComplaintByIds(Long[] complaintIds) {
        return this.removeByIds(Arrays.asList(complaintIds));
    }

    /**
     * 根据订单ID查询投诉信息
     *
     * @param orderUid
     * @return
     */
    @Override
    public List<OrderComplaintVo> getByOrderUid(Long orderUid) {
        return orderComplaintMapper.getByOrderUid(orderUid);
    }


}
